package com.dgshanger.sanhxiaofeisc.mvp.presenter;

import com.dgshanger.sanhxiaofeisc.entity.CheckPwdEntity;
import com.dgshanger.sanhxiaofeisc.entity.PayResultEntity;
import com.dgshanger.sanhxiaofeisc.entity.VipDetailEntity;
import com.dgshanger.sanhxiaofeisc.mvp.contract.VipCardContract;
import com.dgshanger.sanhxiaofeisc.mvp.model.PayModelSettingModel;
import com.dgshanger.sanhxiaofeisc.mvp.model.VipCardModel;
import com.dgshanger.sanhxiaofeisc.ui.person.purse.MembershipCardActivity;
import com.dgshanger.sanhxiaofeisc.ui.recharge.VipCardActivity;
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter;
import com.xx.baseutilslibrary.network.rx.RxHttpObserver;
import com.xx.baseutilslibrary.network.rx.XxBaseHttpObserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * VipCardPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/5/2 11:49.
 */

public class VipCardPresenter extends BaseRxMvpPresenter<VipCardContract.Model, VipCardContract.View> implements VipCardContract.Presenter {

    @NotNull
    @Override
    protected VipCardContract.Model createModel() {
        return new VipCardModel();
    }

    @Override
    public void loadVipDetail() {
        if (getView() == null) {
            return;
        }
        String vipId = getView().getVipId();
        getModel().loadVipDetail(vipId, new RxHttpObserver<VipDetailEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable VipDetailEntity entity) {
                if (getView() == null) {
                    return;
                }
                getView().setData(entity);
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showError(new Throwable(error), true);
            }

            @Override
            public void onLoadingStart() {
                setLoading(true);
            }

            @Override
            public void onLoadingFinish() {
                setLoading(false);
            }
        });
    }

    @Override
    public void vipBuy(String payType, String... payPassword) {
        if (getView() == null) {
            return;
        }
        boolean agreed = getView().isAgreed();
        if (!agreed) {
            getView().showToast("请阅读并同意购买协议");
            return;
        }
        String vipId = getView().getVipId();
        String vipCount = getView().getVipCount();
        String mPayPassword = null;
        if (payPassword != null && payPassword.length != 0) {
            mPayPassword = payPassword[0];
        }
        getModel().vipBuy(vipId, vipCount, payType, mPayPassword, new RxHttpObserver<PayResultEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable PayResultEntity entity) {
                if (getView() == null || entity == null) {
                    return;
                }

                if (VipCardActivity.Companion.getPAY_ALIPAY().equals(payType)) {
                    //阿里支付
                    getView().startAliPay(entity.getAlipay_content());
                } else if (VipCardActivity.Companion.getPAY_WX().equals(payType)) {
                    //微信
                    String payReqString = getModel().getPayReqString(entity.getWx_content());
                    getView().startWeChatPay(payReqString);
                } else if (VipCardActivity.Companion.getPAY_WALLET().equals(payType)) {
                    //零钱
                    getView().dismissPayDialog();
                    getView().startActivityThenFinishSelf(MembershipCardActivity.class);
                }
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }

    @Override
    public void checkPayPassword() {
        new PayModelSettingModel().checkPwd(new XxBaseHttpObserver<CheckPwdEntity>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable CheckPwdEntity entity) {
                if (getView() == null) {
                    return;
                }
                //显示支付弹窗
                if ("0".equals(entity.getPwd_status())) {
                    //未设置密码
                    getView().showToast("请先到个人中心设置支付密码");
                } else {
                    getView().showPayDialog();
                }
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onStart() {
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }

    @Override
    public void getBuyAgreement() {
        getModel().getBuyAgreement(new RxHttpObserver<String>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable String entity) {
                if (getView()==null){
                    return;
                }
                getView().onGetBuyAgreement(entity);
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView()==null){
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog();
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }

}
